﻿<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
<link href="${root!}/lib/ligerUI/skins/Aqua/css/ligerui-all.css" rel="stylesheet" type="text/css" />
<link href="${root!}/lib/ligerUI/skins/ligerui-icons.css" rel="stylesheet" type="text/css" />
<script src="${root!}/lib/jquery/jquery-1.3.2.min.js" type="text/javascript"></script>
<script src="${root!}/lib/ligerUI/js/core/base.js" type="text/javascript"></script>
<script src="${root!}/lib/ligerUI/js/plugins/ligerToolBar.js" type="text/javascript"></script>
<script src="${root!}/lib/ligerUI/js/ligerui.all.js" type="text/javascript"></script>
<script src="${root!}/lib/ligerUI/js/plugins/ligerForm.js"></script>
<script src="${root!}/lib/ligerUI/js/plugins/ligerComboBox.js" type="text/javascript"></script>
<script src="${root!}/lib/ligerUI/js/plugins/ligerMenu.js" type="text/javascript"></script>
<script src="${root!}/lib/ligerUI/js/plugins/ligerTip.js" type="text/javascript"></script>
<script src="${root!}/lib/jquery-validation/jquery.validate.min.js"></script>
<script src="${root!}/lib/jquery-validation/jquery.metadata.js" type="text/javascript"></script>
<script src="${root!}/lib/jquery-validation/messages_cn.js" type="text/javascript"></script>
<script src="${root!}/lib/js/orders.js"></script>
<script> 
var groupicon = "${root!}/lib/ligerUI/skins/icons/communication.gif";
var archives = '${root!}/lib/ligerUI/skins/icons/archives.gif';

var g = null;
var g2= null;
var f = null;
var t = null;
var s = null;
var m = null; 
var r = null;
$(function (){
	/**工具栏*/	
	$("#toptoolbar").ligerToolBar({ items: [
		{ line:true },
		{ text: '添加角色', click: f_AddRole,icon:'memeber'},
		{ line:true },
		{ text: '移除角色', click: f_DelRole,icon:'role'}
	]});
	
	/** 页面布局 */	
	$(function (){
		$("#layout1").ligerLayout({ leftWidth: 200});
	});
	
	 //树
	t = $("#tree1").ligerTree({
		isExpand : 2,
		checkbox: false,
		slide: false,
		nodeWidth: 120,
		url:'${root!}/sys/module/getModuleTree',
		ajaxType: 'get', 
		textFieldName : 'text',
		menuIdFieldName : 'menuid',
		moduleIdFieldName : 'moduleid',
		/* onContextmenu:function (node, e)
            { 
                actionNodeID = node.data.text;
                menu.show({ top: e.pageY, left: e.pageX });
                return false;
            }, */
		onSelect : function(e) {
			//查询对应角色
			var menuid = e.data.menuid;
			if(menuid == null){
				menuid = '0';
			}
			if (e.data.menuid != 0) {
				//用户选择ID传给grid
				var x=[];
				x.push({ name: "menuid", value : menuid},
						{name : "moduleid", value : e.data.moduleid});
				g.set('parms', x);			
				g.loadData();
			}
			//查询模块对应菜单
			if (e.data.moduleid != null && e.data.moduleid != 0) {
				//用户选择ID传给grid
				var y=[]
				y.push({name: "menuid", value : menuid},{name: "moduleid", value: e.data.moduleid})
				g2.set('parms', y)
				g2.loadData();
			}
		}
	});
	
	/**右键菜单*/
	var menu = $.ligerMenu({ top: 100, left: 100, width: 120, items:
	   [/* { text: '增加', click: f_New ,icon: 'add' }, */
	   	{ line: true },
		{ text: '修改', click: f_Modify ,icon:'modify'},
		{ line: true },
		{ text: '删除', click: f_Del ,icon:'delete'},
		{ line: true },
		{ text: '刷新', click: f_Reload ,icon:'refresh'}]
	});
	
	
	/**模块新增*/
	s = $("#ModuleForm").ligerForm({
		 inputWidth: 150, labelWidth: 80, space: 50,validate:{},
			 fields: [
			 { name: "MODULEID", type: "hidden" },
			 { display: "模块名称", name: "NAME", newline: true, type: "text" , group: "模块信息",groupicon: groupicon,validate:{required:true}},
			 { display: "排序", name: "RANK", newline: false, type: "int",validate:{required:true} }	,	 
			 { display: "备注", name: "REMARK", newline: true, type: "text" }]
		 });  
		 
	//初始化时隐藏
	$("#ModuleForm").hide();	
	
	/**菜单新增*/
	m = $("#MenuForm").ligerForm({
		 inputWidth: 150, labelWidth: 80, space: 50,show:false,validate:{},
			 fields: [
			 { name: "MENUID", type: "hidden" },
			 { display: "菜单名称", name: "NAME", newline: false, type: "text",validate:{required:true}, group: "菜单信息",groupicon: groupicon},
			 { display: "菜单URL", name: "URL", newline: true, type: "text",validate:{required:true}},		 
			 { display: "所属模块", name: "SYS_MODULE_ID",  newline: false, type: "combobox",validate:{required:true},
			   options: {			
					valueField: 'ID',
					textField: 'NAME',
					url:'${root!}/sys/module/getModuleCombobox'
				}
			},
			{ display: "序号", name: "SQ", newline: true, type: "number",validate:{required:true} }]
		 });  
	//初始化时隐藏
	$("#MenuForm").hide();
		 
	
	/**角色新增Form*/
	r = $("#RoleForm").ligerForm({
		  inputWidth: 150, labelWidth: 80, space: 50,validate:{},
			 fields: [ 
			 { display: "所属角色", name: "SYS_ROLE_ID",  newline: false,type: "combobox", validate:{required:true}, group: "角色菜单维护",groupicon: groupicon,
			   options: {							
					valueField: 'SYS_ROLE_ID',
					textField: 'ROLE_NAME',
					url:'${root!}/sys/role/getRoleCombobox'
				}
			},		
			{ display: "菜单名称", name: "SYS_MENU_ID",  newline: false, type: "combobox", validate:{required:true},
			   options: {								
					valueField: 'SYS_MENU_ID',
					textField: 'MENU_NAME',
					url:'${root!}/sys/menu/getMenuCombobox'
				}
			}]
		 });  
	//初始化时隐藏
	$("#RoleForm").hide();
	
	//<!--DataGrid-->
	 g = $("#grid1").ligerGrid({
		columns: [
		  { display: '主键', name: 'ID', align: 'left', hide: 1} ,
		  { display: '角色ID', name: 'ROLE_ID', align: 'left', hide: 1} ,
		  { display: '菜单ID', name: 'MENU_ID', align: 'left', hide: 1} ,
		  { display: '角色名称', name: 'ROLE_NAME',align: 'left'} ,
		  { display: '菜单名称', name: 'MENU_NAME',align: 'left'} ,
		  { display: '备注', name: 'ROLE_INFO'}
		], 		
		url: '${root!}/sys/rolemenu/getRoleMenuJson', 
		pageSize: 20, 
		sortName: 'role_id',
		width: '100%', 
		height: '100%', 
		checkbox: false,
		allowUnSelectRow:true,
		method:"post",
		parms:[{name:"pid",value:'0'}]
	});
	 
	 /**菜单信息显示*/
	 $("#navtab1").ligerTab();
	 
	/*  $("#toptoolbar2").ligerToolBar({ items: [
             		{ text: '增加', click: f_New ,icon: 'add' }, 
	   				{ line: true },
					{ text: '修改', click: f_Modify2 ,icon:'modify'},
					{ line: true },
					{ text: '删除', click: f_Del2 ,icon:'delete'}
             	]}); */

	 g2 = $("#grid2").ligerGrid({
			columns: [
			  { display: '菜单ID', name: 'ID', align: 'center', width: 120, hide: 1} ,
			  { display: '序号', name: 'SQ',align: 'center',width: 120},
			  { display: '菜单名称', name: 'NAME', align: 'center',width: 120} ,
			  { display: '菜单URL', name: 'URL', align: 'center',width: 120} ,
			  { display: '所属模块ID', name: 'SYS_MODULE_ID',align: 'center', width: 120, hide: 1} ,
			  { display: '所属模块名', name: 'MODULE_NAME',align: 'center',width: 120} ,
			  { display: '最后更新人ID', name: 'LAST_UPDATED_BY',align: 'center',width: 120, hide: 1},
			  { display: '最后更新人', name: 'LAST_UPDATED_USERNAME',align: 'center',width: 120},
			  { display: '最后更新时间', name: 'LAST_UPDATE_DATE',align: 'center',width: 200}
			],
			url: '${root!}/sys/menu/getMenuMessageJson',
			width: '100%', 
			height: '100%', 
			checkbox: false,
			allowUnSelectRow:true,
			method:"post",
			parms:[{name:"moduelid",value:'0'}]
		});
	 
});


/**新增*/
function f_New()
{
	MenuClear();
	var data = t.getSelected();
	if(data == null){
	 	$.ligerDialog.success('温馨提示：请在模块名下新增。');
	 	return;
	} 	
	if(data['data']['moduleid'] == 0) {//模块新增			
		f_ModuleCreateForm();	
	}else if(data['data']['moduleid'] != 0){ //菜单新增
		f_MenuCreateForm();
	}else{
		$.ligerDialog.success('温馨提示：不能新增下级菜单了。');
	}
}


/**模块创建弹出窗*/
function f_ModuleCreateForm()
{
	var filtercontainer = $("#ModuleForm").width(530).height(200).hide();
		$.ligerDialog.open({
			width: 560, height: 250,target: filtercontainer, isResize: true, top: 50,title:'模块新增',
			buttons: [
			   { text: '确定', onclick: function (item, dialog) { if(f_Module_Save()) dialog.hide(); } },
			   { text: '取消', onclick: function (item, dialog) { ModuleClear(); dialog.hide(); } }
			 ]
		});	
}


/**模块保存*/
function f_Module_Save() {
	var success = false;
	var data = s.getData();
	var jsonStr = [];
	jsonStr.push("ID" + ":" + "'" + stripscript(data['MODULEID']) + "'");
	if (s.valid()) {
		do_ajaxSubmit2('', 'ModuleForm', '${root!}/sys/module/save', jsonStr);
		f_Reload();
		ModuleClear();
		success = true;
	} else {
		s.showInvalid();
	}
	return success;
}

/**模块保存后清空*/
function ModuleClear(){
	var initData = {"MODULEID":"","MODULENAME":"","RANK":"","REMARK":""};
	s.setData(initData);	
}


/** 菜单创建form*/
function f_MenuCreateForm(){
	var filtercontainer = $("#MenuForm").width(530).height(200).hide();
		var selected = t.getSelected();
		//设置模块ID
		m.setData({ PARENT_ID: selected['data']['id']});
		$.ligerDialog.open({
			width: 560, height: 250,target: filtercontainer, isResize: true, top: 50,title:'菜单新增',
			buttons: [
			   { text: '确定', onclick: function (item, dialog) { if(f_Menu_Save()) dialog.hide(); } },
			   { text: '取消', onclick: function (item, dialog) {  MenuClear(); dialog.hide(); } }
			 ]
		});
}


/**菜单保存*/
function f_Menu_Save(){
	var success = false;
	var data = m.getData();
	var jsonStr = [];
	jsonStr.push("MENUID" + ":" + "'" + stripscript(data['MENUID']) + "'");
	if (m.valid()) {
		do_ajaxSubmit2('', 'MenuForm', '${root!}/sys/menu/mySave',jsonStr);
		f_Reload();
		 MenuClear();
		 g2.reload();
		success = true;
	} else {
		m.showInvalid();
	}
	return success;
}

/**菜单保存后清空*/
function MenuClear(){
	var initData = {"MENUID":"","MENUCODE":"","MENUNAME":"","MENUURL":"","PARENT_ID":"","RANK":""};
	m.setData(initData);
}

/**模块和菜单修改*/
function f_Modify(){
	var data = t.getSelected();
	if(data == null){
	 	$.ligerDialog.success('温馨提示：请选择要修改的菜单。');
	 	return;
	} 	
		
	if(data['data']['parentid'] == 0) { //模块修改
		var filtercontainer = $("#ModuleForm").width(530).height(200).hide();
		var selected = t.getSelected();
		//设置要修改的内容
		s.setData({ MODULEID:selected['data']['id'],
					MODULENAME:selected['data']['text'],
					REMARK:selected['data']['remark'],
					RANK:selected['data']['rank']
		});
		$.ligerDialog.open({
			width: 560, height: 250,target: filtercontainer, isResize: true, top: 50,title:'模块修改',
			buttons: [
			   { text: '确定', onclick: function (item, dialog) { if(f_Module_Save()) dialog.hide(); } },
			   { text: '取消', onclick: function (item, dialog) { ModuleClear();dialog.hide(); } }
			 ]
		});			
	}else if(data['data']['parentid'] != 0){ //菜单修改
		var filtercontainer = $("#MenuForm").width(530).height(200).hide();
		var selected = t.getSelected();
		//设置要修改的内容
		m.setData({ MENUID:selected['data']['id'],
					MENUCODE:selected['data']['menucode'],
					MENUURL:selected['data']['menuurl'],
					MENUNAME:selected['data']['text'],
					PARENT_ID:selected['data']['parentid'],
					RANK:selected['data']['rank']
		});
		$.ligerDialog.open({
			width: 560, height: 250,target: filtercontainer, isResize: true, top: 50,title:'菜单修改',
			buttons: [
			   { text: '确定', onclick: function (item, dialog) { if(f_Menu_Save()) dialog.hide(); } },
			   { text: '取消', onclick: function (item, dialog) { MenuClear(); dialog.hide(); } }]
		});
	}else{
		$.ligerDialog.success('温馨提示：没有选择菜单。');
	}
}

/**菜单修改*/
function f_Modify2(){	
	var data = g2.getSelectedRow();
	if(data == null){
	 	$.ligerDialog.success('温馨提示：请选择要修改的行。');
	 	return;
	} 
	var filtercontainer = $("#MenuForm").width(530).height(200).hide();
	var selected = t.getSelected();
		//设置要修改的内容
		m.setData({ MENUID:selected['data']['id'],
					MENUCODE:selected['data']['menucode'],
					MENUURL:selected['data']['menuurl'],
					MENUNAME:selected['data']['text'],
					PARENT_ID:selected['data']['parentid'],
					RANK:selected['data']['rank']
		});
		$.ligerDialog.open({
			width: 560, height: 250,target: filtercontainer, isResize: true, top: 50,title:'菜单修改',
			buttons: [
			   { text: '确定', onclick: function (item, dialog) { if(f_Menu_Save()) dialog.hide(); } },
			   { text: '取消', onclick: function (item, dialog) { MenuClear(); dialog.hide(); } }]
		});
}




/**删除菜单和模块*/
function f_Del(){
	var selected = t.getSelected();
	if(selected == null)
	{
	 	$.ligerDialog.success('温馨提示：请选择要删除的菜单。');
	 	return;
	} 	
	var selectid = selected['data']['id'];
	var selecttext = selected['data']['text'];
	if(selected['data']['parentid'] == 0) //模块删除	
	{
		 if(t.hasChildren(selected['data'])){
			$.ligerDialog.success('温馨提示：模块存在下级菜单,请先删除!');
	 		return;
		 };
	 
		$.ligerDialog.confirm('温馨提示：确认是否删除【'+selecttext+'】？', function (yes){
			if(yes==true){
				do_ajaxSubmit(selectid,'${root!}/admin/module/delete/'+selectid);
				f_Reload();
			};
		});
	}else{ //菜单删除
		$.ligerDialog.confirm('温馨提示：确认是否删除【'+selecttext+'】？', function (yes){
			if(yes==true){
				do_ajaxSubmit(selectid,'${root!}/admin/menu/delete/'+selectid);
				f_Reload();
			};
		});
	}
}

/** 菜单修改*/
function f_Del2(){
	var row = g2.getSelectedRow();
	if (!row) {
		$.ligerDialog.success('温馨提示：请选择要删除的行。');
		return; 
	}
	var selected = t.getSelected();
	var selectid =selected['data']['id'];
	$.ligerDialog.confirm('确认是否删除？', function (yes){
		if(yes==true){
			do_ajaxSubmit(selectid,'${root!}/admin/menu/delete/'+selectid);
			f_Reload();
			g2.reload();
		};
    });
}


/**重新加载树*/
function f_Reload(){
	//重新加载树
	t.clear();	
	t.loadData(null, '${root!}/sys/menu/getMenuTree');
	//重新加载模块下拉框
	var moduleId = $.ligerui.get("MOUDLEID");
	moduleId.reload();
	//重新加载菜单下拉框
	var menuId = $.ligerui.get("MENUID");
	menuId.reload();
	
	var menuData = {"RANK":"","MENUCODE":"","MENUNAME":"","MENUID":"","PARENT_ID":"","MENUURL":""} ;
	var moduleData = {"MODULEID":"","MODULENAME":"","RANK":"","REMARK":""} ;
	m.setData(menuData);
	s.setData(moduleData);
}


/**增加角色*/
function f_AddRole(){
	var selected = t.getSelected();
	if(selected == null){
	 	$.ligerDialog.success('温馨提示：请选择要在哪个节点下新增。');
	 	return;
	} 	
	if(selected['data']['moduleid'] == '0'){
		$.ligerDialog.error('温馨提示：不能在根节点下新增。');
		return;
	}
	var selectid = selected['data']['menuid'];	
	var filtercontainer = $("#RoleForm").width(530).height(200).hide();
	r.setData({ MENU_ID: selectid});//设置模块ID
	$.ligerDialog.open({
		width: 560, height: 250,target: filtercontainer, isResize: true, top: 50,title:'增加角色',
		buttons: [
		   { text: '确定', onclick: function (item, dialog) { if(f_RoleSave()) dialog.hide(); } },
		   { text: '取消', onclick: function (item, dialog) { dialog.hide(); } }
		 ]
	});		
}


/**移除角色*/
function f_DelRole()
{
	var row = g.getSelectedRow();
	if (!row) {
		$.ligerDialog.success('温馨提示：请选择要移除的行。');
		return; 
	}
	delPage(row);
	delete row['MENU_ID'];	
	delete row['ROLE_ID'];
	delete row['MENU_NAME'];	
	delete row['ROLE_NAME'];
	delete row['ROLE_INFO'];
	$.ligerDialog.confirm('确认是否删除？', function (yes){
		if(yes==true){
			do_ajaxSubmit(row,'${root!}/sys/rolemenu/delete');	
		};
    });
}

/**增加角色*/
function f_RoleSave(){
	var success = false;
	var data = r.getData();
	var jsonStr = [];
	//jsonStr.push("ID" + ":" + "'" + stripscript(data['ID']) + "'"); 
	if (r.valid()) {
		do_ajaxSubmit2('', 'RoleForm', '${root!}/sys/rolemenu/saveOrUpdate', jsonStr);
		roleClear();	
		success = true;
	} else {
		r.showInvalid();
	}
	return success;
}


/**清空角色Form*/
function roleClear()
{
	var roleData = {"ROLEID":"","MENUID":"","REMARK":""} ;
	r.setData(roleData);
}

</script>
<style type="text/css"> 
	body{ padding:0px; margin:0; overflow:hidden;}
	#layout1{  width:100%;margin:0; padding:0;  }  
	.l-page-top{ height:80px; background:#f8f8f8; margin-bottom:3px;}
	.tree{
		width:200px;
		height:600px;
		margin: 0px;
		border: 1px solid #ccc;
		overflow: scroll;}
</style>
</head>
<body>
<div id="layout1">
	<div position="left" title="模块名称">
		<div class="tree">
			<ul id="tree1" style="margin-top:3px;"></ul>
		</div>
	</div>
	<div position="center" id="navtab1">
		<div title="角色信息">
			<!--工具栏-->
			<div id="toptoolbar"></div>
			<div id="grid1"></div>
		</div>
		<div title="菜单信息">
			<div id="toptoolbar2"></div>
			<div id="grid2"></div>
		</div>
	</div>
</div> 
<form id="ModuleForm"></form>
<form id="MenuForm"></form>
<form id="RoleForm"></form>

</body>
</html>
